package com.foldingleaf.show.service.impl;

import java.util.Date;
import java.util.List;

import com.foldingleaf.common.utils.uuid.UUID;
import com.foldingleaf.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.foldingleaf.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.foldingleaf.show.domain.SysCollection;
import com.foldingleaf.show.mapper.SysCollectionShowMapper;
import com.foldingleaf.show.domain.SysCollectionShow;
import com.foldingleaf.show.service.ISysCollectionShowService;

/**
 * 藏品出售展示Service业务层处理
 * 
 * @author ajian
 * @date 2022-05-30
 */
@Service
public class SysCollectionShowServiceImpl implements ISysCollectionShowService 
{
    @Autowired
    private SysCollectionShowMapper sysCollectionShowMapper;
    @Autowired
    private ServerConfig serverConfig;
    /**
     * 查询藏品出售展示
     * 
     * @param collectionShowId 藏品出售展示主键
     * @return 藏品出售展示
     */
    @Override
    public SysCollectionShow selectSysCollectionShowByCollectionShowId(Long collectionShowId)
    {
        return sysCollectionShowMapper.selectSysCollectionShowByCollectionShowId(collectionShowId);
    }

    @Override
    public SysCollectionShow selectSysCollectionShowByCollectionShowIdAndIsSell(Long collectionShowId) {
        return sysCollectionShowMapper.selectSysCollectionShowByCollectionShowIdAndIsSell(collectionShowId);
    }


    /**
     * 查询藏品出售展示列表
     * 
     * @param sysCollectionShow 藏品出售展示
     * @return 藏品出售展示
     */
    @Override
    public List<SysCollectionShow> selectSysCollectionShowList(SysCollectionShow sysCollectionShow)
    {
        return sysCollectionShowMapper.selectSysCollectionShowList(sysCollectionShow);
    }

    /**
     * 新增藏品出售展示
     * 
     * @param sysCollectionShow 藏品出售展示
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysCollectionShow(SysCollectionShow sysCollectionShow)
    {



        int rows = sysCollectionShowMapper.insertSysCollectionShow(sysCollectionShow);
        insertSysCollection(sysCollectionShow,false);
        return rows;
    }

    @Override
    public Long selectSysCollectionShowCount(Long collectionShowId) {
        return sysCollectionShowMapper.selectSysCollectionShowCount(collectionShowId);
    }

    /**
     * 修改藏品出售展示
     * 
     * @param sysCollectionShow 藏品出售展示
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysCollectionShow(SysCollectionShow sysCollectionShow)
    {

        return sysCollectionShowMapper.updateSysCollectionShow(sysCollectionShow);
    }

    /**
     * 批量删除藏品出售展示
     * 
     * @param collectionShowIds 需要删除的藏品出售展示主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysCollectionShowByCollectionShowIds(Long[] collectionShowIds)
    {
        sysCollectionShowMapper.deleteSysCollectionByCollectionShowIds(collectionShowIds);
        return sysCollectionShowMapper.deleteSysCollectionShowByCollectionShowIds(collectionShowIds);
    }

    /**
     * 删除藏品出售展示信息
     * 
     * @param collectionShowId 藏品出售展示主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysCollectionShowByCollectionShowId(Long collectionShowId)
    {
        sysCollectionShowMapper.deleteSysCollectionByCollectionShowId(collectionShowId);
        return sysCollectionShowMapper.deleteSysCollectionShowByCollectionShowId(collectionShowId);
    }

    /**
     * 新增藏品详情信息
     * 
     * @param sysCollectionShow 藏品出售展示对象
     */
    public void insertSysCollection(SysCollectionShow sysCollectionShow,boolean isUpdate)
    {
        List<SysCollection> sysCollectionList = sysCollectionShow.getSysCollectionList();
        Long collectionShowId = sysCollectionShow.getCollectionShowId();
        Long collectionNum = sysCollectionShow.getCollectionNum();


        if (StringUtils.isNotNull(sysCollectionList)||collectionNum!=null)
        {
            if (!isUpdate){
                if (collectionNum!=null){

                    List<SysCollection> list = new ArrayList<SysCollection>();
                    for (int i = 0; i < collectionNum; i++) {
                        SysCollection sysCollection = new SysCollection();
                        sysCollection.setCollectionShowId(collectionShowId);
                        sysCollection.setCollectionName(sysCollectionShow.getCollectionName()+"#"+(i+1));
                        sysCollection.setCollectionPrice(sysCollectionShow.getCollectionPrice());
                        sysCollection.setCollectionUrl(sysCollectionShow.getCollectionUrl());
                        sysCollection.setCollectionAuthorId(sysCollectionShow.getCollectionAuthorId());
                        sysCollection.setCollectionDesc(sysCollectionShow.getCollectionDesc());
                        sysCollection.setUserId(1L);
                        sysCollection.setCollectionHashId("0x"+UUID.randomUUID().toString(true));
                        sysCollection.setIsSell("0");
                        sysCollection.setCollectionStatus("0");
                        sysCollection.setColletionTime(new Date().getTime());
                        sysCollection.setCollectionOrderNoHashId("0x"+UUID.randomUUID().toString(true));
                        list.add(sysCollection);
                    }

                    if (list.size() > 0)
                    {
                        sysCollectionShowMapper.batchSysCollection(list);
                    }
                }
            }


            List<SysCollection> list = new ArrayList<SysCollection>();
            for (SysCollection sysCollection : sysCollectionList)
            {
                sysCollection.setCollectionShowId(collectionShowId);
                list.add(sysCollection);
            }
            if (list.size() > 0)
            {
                sysCollectionShowMapper.batchSysCollection(list);
            }
        }
    }
}
